package com.alternatealerts.client;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.kobjects.base64.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.util.Log;


public class DAO {
	private final static String NAMESPACE = "http://tones2text.com/";
	
	// Get the list of stations
	public static ArrayList<Station> getStations(){
		// http://www.tones2text.com/tones/tones.asmx/ListStations
		String METHOD_NAME = "ListStatons";
		String SOAP_ACTION = "http://tones2text.com/ListStations";
	    String URL = "http://www.tones2text.com/tones/tones.asmx?op=ListStations";
		

		ArrayList<Station> stations = new ArrayList<Station>();

		//SoapPrimitive responsesData = null; 

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
 
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
        envelope.dotNet = true; 
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        Log.v("Done",request.toString());
        
        androidHttpTransport.debug = true; 

        try
        {
        	Log.i("WebService", "Try Block");
        	androidHttpTransport.call(SOAP_ACTION, envelope);                          
        	Log.i("WebService", "msg:SoapObject");
        	SoapObject response = (SoapObject)envelope.getResponse();
        	//Object response = (Object)envelope.getResponse();
        	Log.i("WebService", "request dump = "+androidHttpTransport.requestDump);
        	//Object response = (Object)envelope.getResponse();
        	Log.i("WebService", "response dump = "+androidHttpTransport.responseDump);
        	

        	Log.i("WebService", "Response on");



        	SoapObject obj1 = (SoapObject) response.getProperty("diffgram");
        	SoapObject obj2 = (SoapObject) obj1.getProperty(0);
        	for(int i=0;i<obj2.getPropertyCount();i++)//the method getPropertyCount() return the number of rows
        	{
        		SoapObject obj3 = (SoapObject) obj2.getProperty(i);  
        		Log.i("StationID", obj3.getProperty(0).toString());//value of column 1
        		Log.i("StationName", obj3.getProperty(1).toString());//value of column 2

        		stations.add(new Station(obj3.getProperty(1).toString(),Long.parseLong(obj3.getProperty(0).toString())));
        		
        	}      	
        }                                               
        catch (Exception e) 
        {
        	e.printStackTrace();
        	Log.i("WebService", "msg:Exception error");
        	Log.i("WebSerivce", e.getMessage());
        }                   

        return stations;
	}
        
	public static ArrayList<Alert> getStationAlerts(Station station){
		// TODO Retrieve alerts from XML where station ID is equal to
		ArrayList<Alert> alerts = new ArrayList<Alert>();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");	
		String METHOD_NAME = "ListPagesByStation_DataTable";
		String SOAP_ACTION = "http://tones2text.com/ListPagesByStation_DataTable";
	    String URL = "http://www.tones2text.com/tones/tones.asmx?op=ListPagesByStation_DataTable";
	    
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
	    
	    PropertyInfo propertyInfo = new PropertyInfo();
	    propertyInfo.setType(PropertyInfo.INTEGER_CLASS);
	    propertyInfo.setName("stationID");
	    propertyInfo.setValue(station.getStationId());
	    request.addProperty(propertyInfo);
        
	    PropertyInfo propertyQty = new PropertyInfo();
	    propertyQty.setType(PropertyInfo.INTEGER_CLASS);
	    propertyQty.setName("qty");
	    propertyQty.setValue(5);
	    request.addProperty(propertyQty);
	    
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
        envelope.dotNet = true; 
        envelope.implicitTypes = true;
        envelope.setOutputSoapObject(request);
        
        
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
       

        try
        {
        	Log.i("WebService", "Try Block");
        	androidHttpTransport.call(SOAP_ACTION, envelope);
        	SoapObject response = (SoapObject)envelope.getResponse();
        	Log.i("WebService", "request dump = "+androidHttpTransport.requestDump);
        	//Object response = (Object)envelope.getResponse();
        	Log.i("WebService", "response dump = "+androidHttpTransport.responseDump);
        	
        	SoapObject obj1 = (SoapObject) response.getProperty("diffgram");
        	SoapObject obj2 = (SoapObject) obj1.getProperty(0);
        	for(int i=0;i<obj2.getPropertyCount();i++)//the method getPropertyCount() return the number of rows
        	{
        		SoapObject obj3 = (SoapObject) obj2.getProperty(i);  
        		alerts.add(new Alert(station.getStationName(),df.parse(obj3.getProperty(1).toString()),Long.parseLong(obj3.getProperty(0).toString())));
        		
        	}      	
        }                                               
        catch (Exception e) 
        {
        	
        	Log.e("WebService", "msg:Exception error");
        	e.printStackTrace();
        }                   

		return alerts;
	}

	public static ArrayList<Alert> getAlerts() {
		// TODO Retrieve alerts from XML
		ArrayList<Alert> alerts = new ArrayList<Alert>();
		
		return alerts;
	}


	public static byte[] getRecording(long recordingId) {
		// TODO Auto-generated method stub
		String METHOD_NAME = "GetRecordingAudio";
		String SOAP_ACTION = "http://tones2text.com/GetRecordingAudio";
	    String URL = "http://www.tones2text.com/tones/tones.asmx?op=GetRecordingAudio";
		
	    byte[] recording = null;
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
	    PropertyInfo propertyInfo = new PropertyInfo();
	    propertyInfo.setType(PropertyInfo.INTEGER_CLASS);
	    propertyInfo.setName("RecordingID");
	    propertyInfo.setValue((int)recordingId);
	    request.addProperty(propertyInfo);

	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true; 
        envelope.implicitTypes = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true; 

        try
        {
        	androidHttpTransport.call(SOAP_ACTION, envelope);                          
        	SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
        	Log.i("WebService", "request dump = "+androidHttpTransport.requestDump);
        	Log.i("WebService", "response dump = "+androidHttpTransport.responseDump);
        	Log.d("WebService", response.toString());
        	recording = Base64.decode(response.toString());      	
        }                                               
        catch (Exception e) 
        {
        	e.printStackTrace();
        	Log.i("WebService", "msg:Exception error");
        	Log.i("WebSerivce", e.getMessage());
        }                   		
		return recording;
	}


}
